package com.wzq.animal.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzq.animal.back.mapper.RolePermissionMapper;
import com.wzq.animal.back.pojo.RolePermission;
import com.wzq.animal.back.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Transactional
    @Override
    public int setRolePermission(Integer roleId, List<Integer> permissionIds) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,roleId);
        rolePermissionMapper.delete(queryWrapper);

        int count = 0;
        for(Integer pmId : permissionIds){
            RolePermission rp = new RolePermission();
            rp.setRoleId(roleId);
            rp.setPermissionId(pmId);
            rolePermissionMapper.insert(rp);
            count++;
        }
        return count;
    }
}
